home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 338_01 / cmain.c < prev    next >
Text File  |  1989-11-30  |  6KB  |  179 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. char            infile[20],
  26.                 listfile[20],
  27.                 outfile[20];
  28. extern TABLE    tagtable;
  29. int        mainflag, echo;
  30. extern int      total_errors;
  31.  
  32. main(argc,argv)
  33. int     argc;
  34. char    **argv;
  35. {
  36.         printf("\n68000 C Compiler r2.00 for UNIX. Copyright Mathew Brandt 1984/85/86\n");
  37.         printf("Adpated to MSDOS by B Brown, October 1989.\n");
  38.        if( argc == 1 )
  39.        {
  40.             printf("\n\n usage: cc68k -options sourcefile.c\n\n");
  41.             exit(0);
  42.        }
  43.        while(--argc) {
  44.                 if( **++argv == '-')
  45.                         options(*argv);
  46.                 else if( openfiles(*argv)) {
  47.                         printf("Source file : %s\n", infile);
  48.                         printf("Asm file    : %s\n", outfile);
  49.                         printf("Listfile    : %s\n", listfile);
  50.                         lineno = 0;
  51.                         initsym();
  52.                         cgetch();
  53.                         getsym();
  54.                         compile();
  55.                         summary();
  56.                         release_global();
  57.                         closefiles();
  58.                         }
  59.                 }
  60. }
  61.  
  62. int    options(s)
  63. char    *s;
  64. {
  65.     s++;
  66.     if( *s == 'E' || *s == 'e' )  echo = 1;  else echo = 0;
  67.     return 0;
  68. }
  69.  
  70. int     openfiles(s)
  71. char    *s;
  72. {       char    *p;
  73.         int     ofl, lfl;
  74.         strcpy(infile,s);
  75.         strcpy(listfile,s);
  76.         strcpy(outfile,s);
  77.         makename(listfile,".lis");
  78.         makename(outfile,".asm");
  79.         if( (input = fopen(infile,"r")) == 0) {
  80.                 printf(" cant open %s\n",infile);
  81.                 return 0;
  82.                 }
  83.         ofl = creat(outfile,-1);
  84.         if( ofl < 0 )
  85.                 {
  86.                 printf(" cant create %s\n",outfile);
  87.                 fclose(input);
  88.                 return 0;
  89.                 }
  90.         if( (output = fdopen(ofl,"w")) == 0) {
  91.                 printf(" cant open %s\n",outfile);
  92.                 fclose(input);
  93.                 return 0;
  94.                 }
  95.         if( (list = fopen(listfile,"w")) == 0) {
  96.                 printf(" cant open %s\n",listfile);
  97.                 fclose(input);
  98.                 fclose(output);
  99.                 return 0;
  100.                 }
  101.         return 1;
  102. }
  103.  
  104. makename(s,e)
  105. char    *s, *e;
  106. {       while(*s != 0 && *s != '.')
  107.                 ++s;
  108.         while(*s++ = *e++);
  109. }
  110.  
  111. summary()
  112. {       printf("\n -- %d errors found.",total_errors);
  113.         fprintf(list,"\f\n *** global scope symbol table ***\n\n");
  114.         list_table(&gsyms,0);
  115.         fprintf(list,"\n *** structures and unions ***\n\n");
  116.         list_table(&tagtable,0);
  117. }
  118.  
  119. closefiles()
  120. {       fclose(input);
  121.         fprintf(output, "\n*LIBRARY ROUTINES FOR LONG MULTIPLY AND SWITCH STATEMENT FOLLOW\n\n");
  122.         fprintf(output, "*  c%lmul  long signed multiply\n");
  123.         fprintf(output, "*  multiplies two long operands on the stack\n");
  124.         fprintf(output, "*  and returns the result on the stack with no garbage.\n");
  125.         fprintf(output, "*  global  clmul\n");
  126.         fprintf(output, "clmul: \n");
  127.         fprintf(output, "     movem.l d0/d1/d2/d3/,-(a7)          ;save registers\n");
  128.         fprintf(output, "     move.l  20(a7),d0                   ;get parameter 1\n");
  129.         fprintf(output, "     move.w  d0,d2\n");
  130.         fprintf(output, "     move.w  d0,d1\n");
  131.         fprintf(output, "     ext.l   d1\n");
  132.         fprintf(output, "     swap    d1\n");
  133.         fprintf(output, "     swap    d0\n");
  134.         fprintf(output, "     sub.w   d0,d1\n");
  135.         fprintf(output, "     move.w  26(a7),d0             ;get msw of parameter 2\n");
  136.         fprintf(output, "     move.w  d0,d3\n");
  137.         fprintf(output, "     ext.l   d3\n");
  138.         fprintf(output, "     swap    d3\n");
  139.         fprintf(output, "     sub.l   24(a7),d3             ;subtract lsw of parameter 2\n");
  140.         fprintf(output, "     muls    d0,d1\n");
  141.         fprintf(output, "     muls    d2,d3\n");
  142.         fprintf(output, "     add.w   d1,d3\n");
  143.         fprintf(output, "     muls    d2,d0\n");
  144.         fprintf(output, "     swap    d0\n");
  145.         fprintf(output, "     sub.w   d3,d0\n");
  146.         fprintf(output, "     swap    d0\n");
  147.         fprintf(output, "     move.l  d0,24(a7)\n");
  148.         fprintf(output, "     move.l  16(a7),20(a7)          ;move return address\n");
  149.         fprintf(output, "     movem.l (a7)+,d0/d1/d2/d3/     ;restore registers\n");
  150.         fprintf(output, "     add.w   #4,a7                  ;adjust stack\n");
  151.         fprintf(output, "     rts\n");
  152.         fprintf(output, "\n\n*       c%switch - execute c switch statement\n");
  153.         fprintf(output, "*       the switch table is encoded as follows:\n");
  154.         fprintf(output, "*               long    label1,case1\n");
  155.         fprintf(output, "*               long    label2,case2\n");
  156.         fprintf(output, "*               long    label3,case3\n");
  157.         fprintf(output, "*               ... for all cases\n");
  158.         fprintf(output, "*               long    0,defaultcase\n");
  159.         fprintf(output, "*\n*       the case variable is passed in d0\n");
  160.         fprintf(output, "*       global  c%switch\n");
  161.         fprintf(output, "cswitch:\n");
  162.         fprintf(output, "        move.l  (a7)+, a0              ;get table address\n");
  163.         fprintf(output, "csw1:\n");
  164.         fprintf(output, "        move.l  (a0)+, a1              ;get a label\n");
  165.         fprintf(output, "        move.l  a1, d1                 ;test it for default\n");
  166.         fprintf(output, "        beq     csw2                   ;jump if default case\n");
  167.         fprintf(output, "        cmp.l   (a0)+,d0               ;see if this case\n");
  168.         fprintf(output, "        bne     csw1                   ;next case if not\n");
  169.         fprintf(output, "        jmp     (a1)                   ;jump to case\n");
  170.         fprintf(output, "csw2:\n");
  171.         fprintf(output, "        move.l  (a0), a0              ;get default address\n");
  172.         fprintf(output, "        jmp     (a0)                   ;jump to default case\n\n");
  173.  
  174.         fprintf(output,"\n\tEND\n\n");
  175.         fclose(output);
  176.         fclose(list);
  177. }
  178.  
  179.